﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.BackgroundJobs;
using Abp.IO.Extensions;
using Abp.Runtime.Session;
using Abp.UI;
using Abp.Web.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using pandx.Mulan.Authorization;
using pandx.Mulan.Authorization.Users.Importing;
using pandx.Mulan.Authorization.Users.Importing.Dto;
using pandx.Mulan.Helpers;
using pandx.Mulan.Storage;

namespace pandx.Mulan.Controllers
{
    public abstract class UserControllerBase : MulanControllerBase
    {
        // protected readonly IBinaryObjectManager BinaryObjectManager;
        // protected readonly IBackgroundJobManager BackgroundJobManager;

        // protected UserControllerBase(IBinaryObjectManager binaryObjectManager,
        //     IBackgroundJobManager backgroundJobManager)
        // {
        //     BinaryObjectManager = binaryObjectManager;
        //     BackgroundJobManager = backgroundJobManager;
        // }

        private readonly FileUploadHelper _fileUploadHelper;
        private readonly IHttpContextAccessor _httpContextAccessor;

        protected UserControllerBase(FileUploadHelper fileUploadHelper, IHttpContextAccessor httpContextAccessor)
        {
            _fileUploadHelper = fileUploadHelper;
            _httpContextAccessor = httpContextAccessor;
        }

        [AbpAuthorize(AppPermissions.Pages_Administration_Users_Import)]
        [HttpPost]
        public async Task<JsonResult> ImportFromExcel()
        {
            try
            {
                // var file = Request.Form.Files.First();
                // if (file == null)
                // {
                //     throw new UserFriendlyException("文件不存在");
                // }
                //
                // if (file.Length > 1048576 * 100)
                // {
                //     throw new UserFriendlyException("文件大小超过 100M");
                // }
                //
                // byte[] fileBytes;
                // using (var stream = file.OpenReadStream())
                // {
                //     fileBytes = stream.GetAllBytes();
                // }
                //
                // var tenantId = AbpSession.TenantId;
                // var fileObject = new BinaryObject(tenantId, fileBytes, $"{DateTime.UtcNow} 从Excel导出的文件");
                //
                // await BinaryObjectManager.SaveAsync(fileObject);
                //
                // await BackgroundJobManager.EnqueueAsync<ImportUsersFromExcelJob, ImportUsersFromExcelJobArgs>(
                //     new ImportUsersFromExcelJobArgs
                //     {
                //         TenantId = tenantId,
                //         BinaryObjectId = fileObject.Id,
                //         User = AbpSession.ToUserIdentifier()
                //     });
                //
                // return Json(new AjaxResponse(new { }));
                var results = new List<UploadFileResult>();
                var context = _httpContextAccessor.HttpContext;
                await _fileUploadHelper.UploadPartialFile(context, results, SaveType.B);
                return Json(results);
            }
            catch (UserFriendlyException exception)
            {
                return Json(new AjaxResponse(new ErrorInfo(exception.Message)));
            }
        }
    }
}